<template>
	<view>
		<ad-rewarded-video ref="adRewardedVideo" adpid="1104241230" :url-callback="urlCallback" :preload="true" :loadnext="false" :disabled="true"
			v-slot:default="{loading, error}" @load="onadload" @close="onadclose" @error="onaderror">
			<view class="ad-error" v-if="error">{{error}}</view>
		</ad-rewarded-video>
		<button type="primary" class="btn-adv" :disabled="isLoading" :loading="isLoading" @click="showAd">显示广告</button>
	</view>
</template>

<script>
	export default {
    props:["uid"],
		data() {
			return {
				isLoading: false,
				error: "",
        urlCallback:{
          userId: this.uid,
          extra: {}
        }
			}
		},
		onReady() {
			this.isLoading = true;
			this.$refs.adRewardedVideo.load();
		},
		methods: {
			showAd() {
				if (this.isLoading) {
					return
				}
				this.$refs.adRewardedVideo.show();
			},
			onadload(e) {
				this.isLoading = false;
				console.log('广告数据加载成功');
			},
			onadclose(e) {
				const detail = e.detail
				// 用户点击了【关闭广告】按钮
				if (detail && detail.isEnded) {
					// 正常播放结束
					console.log("onClose " + detail.isEnded);

				} else {
					// 播放中途退出
					console.log("onClose " + detail.isEnded);
				}
				//this.isLoading = true;
				//this.$refs.adRewardedVideo.load();
			},
			onaderror(e) {
				// 广告加载失败
				console.log(e.detail);
				this.isLoading = false;
			},

			// 观看视频获取积分
			// GetList() {
			// 	advertList().then((res) => {
			// 		console.log(res)
			// 		if (res.code === 200) {
			// 			this.$util.Tips({
			// 				title: res.message
			// 			});
			// 		}
			// 	}).catch(err => {
			// 		this.$util.Tips({
			// 			title: err
			// 		});
			// 		console.log(err)
			// 	});
			// }
		}
	}
</script>

<style>
	.ad-error {
		color: orangered;
		margin-top: 5px;
	}
	.btn-adv {
		position: fixed;
		width: 80px;
		height: 35px;
		border-radius: 20px;
		background-color: blue;
		color: #fff;
		font-size: 12px;
		text-align: center;
		line-height: 35px;
		margin: auto;
		margin-top: 30px;
		left: 0;
		right: 0;
		top: 10;
	}
</style>
